package com.tuyan.aixiang.activity.login;

import android.content.Intent;
import android.text.InputType;
import android.view.View;
import android.widget.TextView;

import com.tuyan.aixiang.R;
import com.tuyan.aixiang.activity.home.HomeActivity;
import com.tuyan.aixiang.common.AppData;
import com.tuyan.aixiang.common.BaseActivity;
import com.tuyan.aixiang.common.Constant;
import com.tuyan.aixiang.component.ConfirmDialog;
import com.tuyan.aixiang.enums.LaunchMode;

import androidx.appcompat.widget.AppCompatEditText;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * 登录页
 */
public class LoginActivity extends BaseActivity<LoginPresenter> implements Login.View {

    @BindView(R.id.et_username)
    AppCompatEditText etUsername;

    @BindView(R.id.et_password)
    AppCompatEditText etPassword;

    private ConfirmDialog confirmDialog;

    @BindView(R.id.tv_login_mode)
    TextView tvLoginMode;

    @BindView(R.id.tv_register)
    TextView tvRegister;


    @Override
    public void bindView() {
        super.bindView();
        AppData.clearNoticeCountChangeCallback();
        AppData.clearUserDataChangeCallback();
        fitSystemWindow(R.color.colorPure);
        Integer willMode = getIntent().getIntExtra("willMode", LaunchMode.CONSUMER.getValue());
        if(willMode.equals(LaunchMode.CONSUMER.getValue())) {
            tvLoginMode.setText(LaunchMode.CONSUMER.getName());
            tvRegister.setVisibility(View.VISIBLE);
        }

        if(willMode.equals(LaunchMode.ENTERPRISE.getValue())) {
            tvLoginMode.setText(LaunchMode.ENTERPRISE.getName());
            tvRegister.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected LoginPresenter createPresenter() {
        return new LoginPresenter();
    }

    @OnClick(R.id.ll_btn_login)
    public void login() {
        String username = getFormText(etUsername);
        String password = getFormText(etPassword);
        iPresenter.doLogin(username, password);
    }

    @OnClick(R.id.iv_back)
    public void back() {
        this.finish();
    }

    @OnClick(R.id.tv_register)
    public void register() {
        if(AppData.getLanuchMode().equals(LaunchMode.CONSUMER.getValue())) {
            openWeb(Constant.ACCOUNT_REGISTER);
        }
    }

    @OnClick(R.id.tv_restore)
    public void restore() {
        openWeb(Constant.ACCOUNT_RESTORE);
    }

    @OnClick(R.id.iv_eye)
    public void eye() {
        if (etPassword.getInputType() == InputType.TYPE_TEXT_VARIATION_PASSWORD) {
            etPassword.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
        } else {
            etPassword.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
        }
    }

    @Override
    public void showInitialPwdDialog() {
        openWeb(Constant.CHANGE_PWD);
        this.confirmDialog.setOnConfirm(() -> {
            openWeb(Constant.CHANGE_PWD);
        });
        this.confirmDialog.show();
    }

    @Override
    public void loginSuccess() {
//        HomeActivity.start(this);
        Intent intent = new Intent(this, HomeActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }

}
